package main

import (
	"github.com/julienschmidt/httprouter"
	"net/http"
	"video/streamserver/handler"
	"video/streamserver/limit"
)

func main() {
	r := RegisterHandlers()
	route := NewMiddleWareHandler(r, 12)
	http.ListenAndServe(":9000", route)
}

type middleWareHandler struct {
	r *httprouter.Router
	l *limit.ConnLimiter
}

func (m *middleWareHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	if !m.l.GetConn() {

	}
	m.r.ServeHTTP(w, req)
	defer m.l.ReleaseConn()
}

func NewMiddleWareHandler(r *httprouter.Router, cc int) http.Handler {
	m := middleWareHandler{}
	m.r = r
	m.l = limit.NewConnLimiter(cc)
	return &m
}

func RegisterHandlers() *httprouter.Router {
	route := httprouter.New()
	route.GET("/videos/:vid-id", handler.StreamHandler)
	route.POST("/upload/:vid-id", handler.UploadHandler)
	return route
}
